/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule;

import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpTopic;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpSet;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.services.HelpService;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author Breno Leite
 */
public class HelpSummaryParser {

    private HelpSet rootHelpSet = new HelpSet();

    public HelpSet loadHelpSetsFromDirectory(File targetFolder) {
        loadTableOfContents(rootHelpSet, targetFolder);
        return rootHelpSet;
    }

    public HelpSet loadHelpSetsFromSummaryFile(File summaryFile) {
        HelpSet root = new HelpSet();

        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(summaryFile);
            Element aux = (Element) document.getRootElement().elements().get(0);
            HelpSet temp = new HelpSet();

            iterateOverElement(temp, aux);

        } catch (DocumentException ex) {
            ex.printStackTrace();
        }

        return root;
    }

    private void iterateOverElement(HelpSet parentHelpSet, Element helpSetElement) {
        for (Object elementObject : helpSetElement.elements()) {
            Element element = (Element) elementObject;

            if (element.getName().equals("help-set")) {
                HelpSet helpSet = new HelpSet();
                helpSet.setDescription(element.attributeValue("description"));
                helpSet.setTitle(element.attributeValue("title"));
                parentHelpSet.addHelpSet(helpSet);
                iterateOverElement(helpSet, element);
            }

            if (element.getName().equals("help-topic")) {
                HelpTopic helpTopic = new HelpTopic();
                helpTopic.setTitle(element.attributeValue("title"));
                helpTopic.setDescription(element.attributeValue("description"));
                helpTopic.setPath(element.attributeValue("path"));
                helpTopic.setKeywords(element.attributeValue("keywords"));
                parentHelpSet.addHelpTopic(helpTopic);
            }
        }
    }

    private void loadTableOfContents(HelpSet parentSet, File targetFile) {
        if (targetFile.isDirectory()) {
            HelpSet helpSet = new HelpSet();

            for (File temp : targetFile.listFiles()) {
                if (temp.isFile() && temp.getName().equals("folder-info.properties")) {
                    loadHelpSetInfo(helpSet, temp);
                } else {
                    loadTableOfContents(helpSet, temp);
                }
            }

            if (parentSet == null) {
                rootHelpSet.addHelpSet(helpSet);
            } else {
                parentSet.addHelpSet(helpSet);
            }
        } else {
            HelpTopic helpTopic = HelpService.retriveFileInfo(targetFile);

            if (parentSet == null) {
                rootHelpSet.addHelpTopic(helpTopic);
            } else {
                parentSet.addHelpTopic(helpTopic);
            }
        }
    }

    private void loadHelpSetInfo(HelpSet helpSet, File propertiesFile) {
        if (propertiesFile.isFile() && propertiesFile.getName().equals("folder-info.properties")) {
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream(propertiesFile));
                helpSet.setTitle(properties.getProperty("folder.name"));
                helpSet.setDescription(properties.getProperty("folder.description"));
            } catch (IOException exception) {
            }
        } else {
            helpSet.setTitle(helpSet.getPath().getName());
        }
    }
}
